/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import service.HelloService;

/** 
 *
 * @author amjaalto
 */


public class HelloController extends SimpleFormController {
    
    private HelloService helloService;
    
    public HelloController() {
        //Initialize controller properties here or 
        //in the Web Application Context

        setCommandClass(Name.class);
        setCommandName("name"); //In this case, the command is the form 
                                //object with request parameters bound onto it WAT
        setSuccessView("helloView");
        setFormView("nameView");
    }
    
    @Override
    protected ModelAndView onSubmit(
    HttpServletRequest request, 
    HttpServletResponse response, 
    Object command, 
    BindException errors) throws Exception {
        
    Name name = (Name) command;
    ModelAndView mv = new ModelAndView(getSuccessView());
    mv.addObject("helloMessage", helloService.sayHello(name.getValue()));
    return mv;

    }
    public void setHelloService(HelloService helloService) {
        
        this.helloService = helloService;
        
    }
}
